package com.lychee.lycheeapp;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class RegisterActivity extends AppCompatActivity {

    @BindView(R.id.mEditUserName)
    EditText mEditUserName;
    @BindView(R.id.mEditPassword)
    EditText mEditPassword;
    @BindView(R.id.mTvRegister)
    TextView mTvRegister;
    @BindView(R.id.mEditSex)
    EditText mEditSex;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        mContext = this;
        int randomUserName = new Random().nextInt(9999);
        int randomPassowrd = new Random().nextInt(9999);
        int randomSex = new Random().nextInt(2);
        mEditUserName.setText("用户名" + randomUserName);
        mEditPassword.setText("密码" + randomPassowrd);
        mEditSex.setText("性别" + randomSex);
    }

    @OnClick(R.id.mTvRegister)
    public void onViewClicked() {
        String userName = mEditUserName.getText().toString().trim();
        String password = mEditPassword.getText().toString().trim();
        String sex = mEditSex.getText().toString().trim();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password) || TextUtils.isEmpty(sex)) {
            Toast.makeText(mContext, "请输入完整", Toast.LENGTH_LONG).show();
        } else {
            OkHttpUtils.post()
                    .addParams("userName", userName)
                    .addParams("password", password)
                    .addParams("sex", sex)
                    .url(IUrlInterface.baseUrl + IUrlInterface.registerUser)
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Toast.makeText(mContext, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.i(getClass().getSimpleName(), "onResponse: " + response);
                    System.out.println(response);
                    Toast.makeText(mContext, response, Toast.LENGTH_SHORT).show();
                }
            });
        }


    }
}
